package com.example.experiment4

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.GridView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class CardGameActivity : AppCompatActivity() {

    private lateinit var deck: Deck
    private lateinit var cards: MutableList<Card>
    private var score = 0
    private var highScore = 0
    private var indexOfOneAndOnlyFaceUpCard: Int? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_card_game)

        highScore = intent.getIntExtra("highScore", 0)
        score = 0

        deck = Deck()
        cards = mutableListOf()
        for (i in 0 until deck.size()) {
            cards.add(deck.dealCard()!!)
        }

        val gridView = findViewById<GridView>(R.id.gv_cards)
        val adapter = CardAdapter(this, cards) { position ->
            handleCardClick(position)
        }
        gridView.adapter = adapter

        findViewById<TextView>(R.id.tv_score).text = score.toString()

        // 重置游戏
        findViewById<Button>(R.id.btn_reset_game).setOnClickListener {
            resetGame()
            adapter.notifyDataSetChanged()
            findViewById<TextView>(R.id.tv_score).text = score.toString()
        }

        // 退出游戏
        findViewById<Button>(R.id.btn_exit).setOnClickListener {
            val intent = Intent()
            intent.putExtra("highScore", if (score > highScore) score else highScore)
            setResult(RESULT_OK, intent)
            finish()
        }
    }

    private fun handleCardClick(position: Int) {
        val card = cards[position]
        if (card.isMatched) return

        if (indexOfOneAndOnlyFaceUpCard == null) {
            // 重置所有未匹配的牌为背面
            for (i in cards.indices) {
                if (!cards[i].isMatched) {
                    cards[i].isFaceUp = false
                }
            }
            indexOfOneAndOnlyFaceUpCard = position
        } else {
            val otherPosition = indexOfOneAndOnlyFaceUpCard
            if (otherPosition == position) return // 点击同一张牌，不处理
            indexOfOneAndOnlyFaceUpCard = null

            if (card == cards[otherPosition!!]) {
                // 匹配成功
                card.isMatched = true
                cards[otherPosition].isMatched = true
                score += 2
            } else {
                // 匹配失败
                score--
            }
        }
        card.isFaceUp = true
        (findViewById<GridView>(R.id.gv_cards).adapter as CardAdapter).notifyDataSetChanged()
        findViewById<TextView>(R.id.tv_score).text = score.toString()
    }

    private fun resetGame() {
        deck.reset()
        cards.clear()
        for (i in 0 until deck.size()) {
            cards.add(deck.dealCard()!!)
        }
        score = 0
        indexOfOneAndOnlyFaceUpCard = null
    }

    override fun onBackPressed() {
        val intent = Intent()
        intent.putExtra("highScore", if (score > highScore) score else highScore)
        setResult(RESULT_OK, intent)
        finish()
    }
}